package com.avic.rong.activity.no;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.utils.OkHttpUtils;
import com.avic.rong.utils.SDCardUtils;
import com.avic.rong.wedgit.permissionschecker.PermissionChecker;
import com.github.barteksc.pdfviewer.PDFView;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VoicePDFActivity extends AppCompatActivity  {

    public static String ARG_URL = "arg_url";
    private String url = null;

    private PermissionChecker permissionChecker;
    private String[] PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @BindView(R.id.pdfView)
    PDFView pdfView;
    @BindView(R.id.tv_tip)
    TextView tv_tip;

    private  String savePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_pdf);
        ButterKnife.bind(this);

        if (getIntent() != null) {
            url = getIntent().getStringExtra(ARG_URL);
        }
        savePath = SDCardUtils.getSDCardPath() + "rong" + File.separator;
        permissionChecker = new PermissionChecker(VoicePDFActivity.this);

        if (permissionChecker.isLackPermissions(PERMISSIONS)) {
            permissionChecker.requestPermissions();
        }else{
            downloadFile(url,savePath);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PermissionChecker.PERMISSION_REQUEST_CODE:
                if (permissionChecker.hasAllPermissionsGranted(grantResults)) {
                    downloadFile(url,savePath);
                } else {
                    permissionChecker.showDialog();
                }
                break;
        }
    }



    private void downloadFile(String path,String savePath){
        OkHttpUtils.build().download(path, savePath, new OkHttpUtils.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(File file) {
//                tv_tip.setVisibility(View.GONE);
                pdfView.fromFile(file).load();
            }
            @Override
            public void onDownloading(int progress) {
//                tv_tip.setVisibility(View.VISIBLE);
//                tv_tip.setText("正在加载中...");
//                tvPage.setText("正在加载("+progress+"/100)");
            }
            @Override
            public void onDownloadFailed() {
//                Log.i("JAVA", "onDownloadFailed");
//                tvPage.setText("加载失败..");
//                tv_tip.setVisibility(View.VISIBLE);
//                tv_tip.setText("加载失败...");
                handler.sendEmptyMessage(0);
            }
        });
    }


    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0){
                tv_tip.setVisibility(View.VISIBLE);
                tv_tip.setText("加载失败...");
            }
        }
    };


    @OnClick(R.id.img_back)
    public void back(){
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static Intent queryIntent(Context context, String url) {
        Intent intent = new Intent(context, VoicePDFActivity.class);
        intent.putExtra(ARG_URL, url);
        return intent;
    }
}
